package com.sofwin.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sofwin.pojo.SysRole;
import com.sofwin.pojo.SysRoleExample;
import com.sofwin.pojo.SysRoleMenu;
import com.sofwin.system.mapper.SysRoleMapper;
import com.sofwin.system.mapper.SysRoleMenuMapper;
import com.sofwin.system.service.RoleMenuService;
import com.sofwin.system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @packageName: com.sofwin.system.service.impl
 * @user: andyliu
 * @date: 2021/12/16 15:46
 * @email 115176513@qq.com
 * @description: TODO
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private SysRoleMapper mapper;
    @Autowired
    private RoleMenuService roleMenuService;
    @Autowired
    private SysRoleMenuMapper roleMenuMapper;

    @Override
    public PageInfo<SysRole> selectByPage(SysRole role, Integer pageNumber, Integer pageSize) {

        SysRoleExample ex = new SysRoleExample();
        SysRoleExample.Criteria criteria = ex.createCriteria();
        if(role!=null){
            if(!StringUtils.isEmpty(role.getRoleName())) {
                criteria.andRoleNameEqualTo(role.getRoleName());
            }
            if(!StringUtils.isEmpty(role.getRolePermission())) {
                criteria.andRolePermissionEqualTo(role.getRolePermission());
            }
            if(role.getStatus()!=null && role.getStatus()!=-1){
                criteria.andStatusEqualTo(role.getStatus());
            }
        }
        PageHelper.startPage(pageNumber,pageSize);
        List<SysRole> roles = mapper.selectByExample(ex);
        return new PageInfo<>(roles);
    }

    @Override
    public boolean deleteByIds(Integer[] ids) {
        return mapper.deleteByIds(ids)==ids.length?true:false;
    }

    @Override
    public boolean deleteById(Integer id) {
        return mapper.deleteByPrimaryKey(id)==1?true:false;
    }

    @Override
    public boolean saveOrUpdate(SysRole role) {
        if(role.getId()!=null){
            return mapper.updateByPrimaryKeySelective(role)==1?true:false;
        }
        return mapper.insertSelective(role)==1?true:false;
    }

    @Override
    public SysRole selectById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean savePermission(Integer roleId, Integer[] ids) {
        try {
            roleMenuService.deleteRoleMenus(roleId);
            for (Integer menuId : ids
            ) {
                if(menuId!=0) {
                    SysRoleMenu roleMenu = new SysRoleMenu();
                    roleMenu.setRoleId(roleId);
                    roleMenu.setMenuId(menuId);
                    roleMenuMapper.insertSelective(roleMenu);
                }
            }
            return true;
        }catch (Exception ex){
            return false;
        }
    }

    /**
     * 根据角色id查询细粒度的权限字符的集合
     * @param roleId
     * @return
     */
    @Override
    public List<String> getRolePermissions(Integer roleId) {
        return mapper.selectRolePermission(roleId);
    }
}
